package com.peanut.controller;

import com.alibaba.fastjson.JSONObject;
import com.peanut.VehicleManagementFeign;
import com.peanut.common.Result;
import com.peanut.config.RedisTabAdvertisement;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@Api
@RequestMapping("advertisement")
public class HomeController {
    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private VehicleManagementFeign vehicleManagementFeign;
    //查询
    @ApiOperation("导航栏")
    //导航栏
    @GetMapping("findAllAdvertisementBrand")
    public Result findAllAdvertisementBrand() {
        if (redisTemplate.hasKey(RedisTabAdvertisement.TAB_ADVERTISEMENT_BRAND)) {
            Result dataResult = (Result) redisTemplate.boundHashOps(RedisTabAdvertisement.TAB_ADVERTISEMENT_BRAND).get(RedisTabAdvertisement.TAB_ADVERTISEMENT_BRAND_VALUE);
            String s = JSONObject.toJSONString(dataResult.getData());
            return Result.OK().setData(s);
        } else {
            Result dataResult = vehicleManagementFeign.FindBrand();
            redisTemplate.boundHashOps(RedisTabAdvertisement.TAB_ADVERTISEMENT_BRAND).put(RedisTabAdvertisement.TAB_ADVERTISEMENT_BRAND_VALUE, dataResult);
            String s = JSONObject.toJSONString(dataResult.getData());
            return Result.OK().setData(s);
        }
    }
}
